#! /usr/bin/env python
#coding=utf-8
#-*-encoding=utf-8
#for get tvshowtime  movie list

from httpservice import HttpService
from fileutil import  FileUtil
from bs4 import BeautifulSoup
from http.cookiejar import LWPCookieJar
import os,sys,io

#特殊字符无法输出,改变输出编码
sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') 

COOKIE_FILE="tv_showtime_cookies"

class TvShowHttpService(HttpService):
	def __init__(self):
		super(TvShowHttpService,self).__init__()
		self.session.cookies = LWPCookieJar(COOKIE_FILE)

#-------------------------------------------------------------------
class  TvShowFileUtil():
	def __init__(self):
		self.config_file = "towatch.html"
		self.file_util = FileUtil()
		pass
	def writefile(self,info):
		self.file_util.write(self.config_file,info)


class TvShowTimeList(object):
	def __init__(self):
		super(TvShowTimeList, self).__init__()
		self.httpservice = TvShowHttpService()
		self.tvshowfileutil = TvShowFileUtil()

	def signin (self,username,password,redirect_path="http://www.tvshowtime.com/en"):
		if not os.path.exists(COOKIE_FILE):
			base_url = "http://www.tvshowtime.com/signin"
			params = {"username":username,"password":password,"redirect_path":redirect_path}
			html = self.httpservice.post(base_url,params)
			self.httpservice.getSession().cookies.save()
		else:
			self.httpservice.getSession().cookies.load()

	def read_list(self):
		base_url = "http://www.tvshowtime.com/en"
		html = self.httpservice.get(base_url)
		# print(html)
		self.tvshowfileutil.writefile(html)


tvshowlist = TvShowTimeList()
tvshowlist.signin("wujiyu115@gmail.com","wujiyufar115")
tvshowlist.read_list()
